package com.dongdongshop.service.impl;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.SpecVO;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.TbSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@com.alibaba.dubbo.config.annotation.Service
public class TbSpecificationServiceImpl  implements TbSpecificationService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public List<TbSpecification> selectAll() {
        List<TbSpecification> specifications = tbSpecificationMapper.selectByExample(null);
        return specifications;
    }
    //增加
    @Override
    public void addSpecBatch(TbSpecification tbSpecification, List<TbSpecificationOption> TbSpecificationOption) {
        tbSpecificationMapper.insertSelective(tbSpecification);
        for (TbSpecificationOption tbSpecificationOption : TbSpecificationOption) {
            tbSpecificationOption.setSpecId(tbSpecification.getId());
            tbSpecificationOptionMapper.insertSelective(tbSpecificationOption);
        }
    }
    //回显
    @Override
    public SpecVO selectById(Integer id) {
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id.longValue());
        List<TbSpecificationOption> specificationOptions=tbSpecificationOptionMapper.selectById(id);
        SpecVO vo = new SpecVO();
          vo.setOptions(specificationOptions);
          vo.setSpecification(tbSpecification);
         return vo;
    }

    @Override
    public void selectBatch(Long[] ids){
        TbSpecificationOptionExample example =new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria1 = example.createCriteria();
        for (Long id : ids) {
            tbSpecificationMapper.deleteByPrimaryKey(id);
            criteria1.andSpecIdEqualTo(id);
            tbSpecificationOptionMapper.deleteByExample(example);
        }
    }

    @Override
    public void updateSpec(TbSpecification tbSpecification, List<TbSpecificationOption> tbS) {
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        for (int i = 0; i < tbS.size(); i++) {
            tbSpecificationOptionMapper.updateByPrimaryKeySelective(tbS.get(i));
        }
    }
}
